



import pymongo
from pymongo import MongoClient

class MG(object):

	def __init__(self):
		self.client = MongoClient("172.17.0.2",27017)
		self.db = self.client.admin
		self.db.authenticate("xx","pwd",mechanism="SRAM-SHA-1")
		self.db = self.client["dbName"]


class BaseHandle(object):

	@staticmethod
	def insert_one(collection,data):
		res = collection.insert_one(data)
		return res.inserted_id 

	@staticmethod
	def insert_many(collection,data_list):
		res = collection.insert_many(data_list)
		return res.inserted_ids

	@staticmethod
	def find_one(collection,data,data_field={}):
		if len(data_field):
			res = collection.find_one(data,data_field)
		else:
			res = collection.find_one(data)
		return res

	@staticmethod
	def find_many(collection,data,data_field={}):
		if len(data_field):
			res = collection.find(data,data_field)
		else:
			res = collection.find(data)
		return res 

	@staticmethod
	def update_one(collection,data_condition,data_set):
		res = collection.update_one(data_condition,data_set)
		return res

	@staticmethod 
	def update_many(collection,data_condition,data_set):
		res = collection.update_many(data_condition,data_set)
		return res

	@staticmethod
	def replace_one(collection,data_condition,data_set):
		res = collection.replace_one(data_condition,data_set)
		return res 

	@staticmethod
	def delete_one(collection,data_condition):
		res = collection.delete_one(data_condition)
		return res 

	@staticmethod
	def delete_many(collection,data_condition):
		res = collection.delete_many(data_condition)
		return res 

class DBBase(object):

	def __init__(self,collection):
		self.mg = MG()
		self.collection = self.mg.db[collection]

	def insert_one(self,data):
		res = BaseHandle.insert_one(self.collection,data)
		return res 

	def insert_many(self,data_list):
		res = BaseHandle.insert_many(self.collection,data_list)
		return res

	def find_one(self,data,data_field={}):
		res = BaseHandle.find_one(self.collection,data,data_field)
		return res 

	def find_many(self,data={},data_field={}):
		res = BaseHandle.find_many(self.collection,data,data_field)
		return res

	def find_in(self,feild,item_list,data_field={}):
		data = dict()
		data[field] = {"$in":item_list}
		res = BaseHandle.find_many(self.collection,data,data_field)
		return res 

	def find_or(self,data_list,data_field={}):
		"""
		db.user.find(
		{"$or":[{"name":"leo"},{"age":12}]}
		)
		"""
		data = dict()
		data["$or"] = data_list
		res = BaseHandle.find_many(self.collection,data,data_field)
		return res 

	def find_between(self,feild,value1,value2,data_field={}):
		data = dict()
		data[field] = {"$gt":value1,"$lt":value2}
		res = BaseHandle.find_many(self.collection,data,data_field)
		return res

	def find_more(self,feild,value,data_field={}):
		data=dict()
		data[field] = {"$gt":value}
		res = BaseHandle.find_many(self.collection,data,data_field)
		return res 

	def find_like(self,feild,value,data_field={}):
		data = dict()
		data[field] = {"$regex":".*"+value+".*"}
		res = BaseHandle.find_many(self.collection,data,data_field)
		return res 

	def delete_one(self,data):
		res = BaseHandle.delete_one(self.collection,data)
		return res 

	def delete_many(self,data):
		res = BaseHandle.delete_many(self.collection,data)
		return res 

	def query_limit(self,query,num):
		res = query.limit(num)
		return res 

	def query_skip(self,query,num):
		res = query.skip(num)
		return res 

	def query_sort(self,query,data):
		res = query.sort(data)
		return res 

	def query_count(self,query):
		"""
		db.user.find().count()
		"""
		res = query.count()
		return res 

	def query_page(self,query,skip_num,limit_num):
		res = query.skip(skip_num).limit(limit_num)
		return res 

#集合
class DBStudent(DBBase):

	def __init__(self):
		super(DBStudent,self).__init__("student")

student = DBStudent()
student.insert_one({})